//
//  StatusToolBar.m
//  CMJWeibo
//
//  Created by BH on 16/1/5.
//
//

#import "StatusToolBar.h"
#import "StatusModel.h"
#import "UIImage+MJ.h"
#import "UIView+MJ.h"

@interface StatusToolBar ()

@property(nonatomic,strong)NSMutableArray *btns;
@property(nonatomic,strong)NSMutableArray *dividers;
@property(nonatomic,strong)UIButton *repostBtn;
@property(nonatomic,strong)UIButton *commentBtn;
@property(nonatomic,strong)UIButton *zanBtn;

@end

@implementation StatusToolBar

-(NSMutableArray *)btns
{
    if (!_btns) {
        self.btns = [NSMutableArray array];
    }
    return _btns;
}

-(NSMutableArray *)dividers
{
    if (!_dividers) {
        self.dividers = [NSMutableArray array];
    }
    return _dividers;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.image = [UIImage resizableImage:@"common_card_bottom_background"];
        
        //添加按钮
        self.repostBtn = [self setBtnWithIcon:@"timeline_icon_retweet" title:@"转发" tag:201];
        self.commentBtn = [self setBtnWithIcon:@"timeline_icon_comment" title:@"评论" tag:202];
        self.zanBtn = [self setBtnWithIcon:@"timeline_icon_unlike" title:@"赞" tag:203];
        
        //添加分割线,调用两次，添加两根分割线
        [self setDivider];
        [self setDivider];
    }
    return self;
}

/**
 *  添加按钮
 *
 *  @param icon 图片
 *  @param title 标题
 *  @param tag 标记值
 */
-(UIButton *)setBtnWithIcon:(NSString *)icon title:(NSString *)title tag:(NSInteger)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = tag;
    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [button setBackgroundImage:[UIImage resizableImage:@"common_card_bottom_background_highlighted"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenHighlighted = NO;
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [self addSubview:button];
    
    //将button添加到数组中
    [self.btns addObject:button];
    
    return button;
}

/**
 *  添加分割线
 */
-(void)setDivider
{
    UIImageView *divider = [[UIImageView alloc] init];
    divider.image = [UIImage imageWithName:@"timeline_card_bottom_line"];
    [self addSubview:divider];
    
    [self.dividers addObject:divider];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置按钮frame
    NSInteger btnCount = self.btns.count;
    CGFloat btnW = self.width/btnCount;
    CGFloat btnH = self.height;
    for (int i=0; i<btnCount; i++) {
        UIButton *button = self.btns[i];
        button.width = btnW;
        button.height = btnH;
        button.y = 0;
        button.x = btnW * i;
    }
    
    //设置分割线frame
    for (int i=0; i<self.dividers.count; i++) {
        UIImageView *imageView = self.dividers[i];
        imageView.width = 1;
        imageView.height = btnH;
        imageView.centerX = (i+1)*btnW;
        imageView.centerY = btnH * 0.5;
    }
}

-(void)setStatusM:(StatusModel *)statusM
{
    _statusM = statusM;
    
    [self setBtnTitle:self.repostBtn count:statusM.reposts_count originalTitle:@"转发"];
    
    [self setBtnTitle:self.commentBtn count:statusM.comments_count originalTitle:@"评论"];
    
    [self setBtnTitle:self.zanBtn count:statusM.attitudes_count originalTitle:@"赞"];
}

/**
 *  设置按钮的文字
 *  
 *  @param button 要设置的按钮
 *  @param count  数量
 *  @param originalTitle 没有数字时显示的文字
 */
-(void)setBtnTitle:(UIButton *)button count:(NSInteger)count originalTitle:(NSString *)originalTitle
{
    if (count) {
        //非0,有数量
        NSString *title = nil;
        if (count>=10000) {
            //数量大于10000
            title = [NSString stringWithFormat:@"%.1f万",count/10000.0];
            //去掉.0
            title = [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
        }else{
            //不到10000
            title = [NSString stringWithFormat:@"%ld",count];
        }
        [button setTitle:title forState:UIControlStateNormal];
    }else{
        //没有数量
        [button setTitle:originalTitle forState:UIControlStateNormal];
    }
}

/** button点击事件 */
-(void)buttonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(statusToolBar:buttonTag:)]) {
        [self.delegate statusToolBar:self buttonTag:button.tag];
    }
}

@end
